#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "PWM.h"
#include "light_sensor.h"

int main(void)
{
	OLED_Init();
	PWM_Init();
	LightSensor_Init();
	
	OLED_ShowString(1, 1, "Level:");
	OLED_ShowString(2, 1, "Light:");
	OLED_ShowNum(1, 7, 0, 4);
	
	OLED_ShowString(3, 1, "Close");
	
	uint8_t isOpen = 0;
	uint16_t level = 0;
	
	uint8_t lightStatus = LightSensor_GetStatus();
	OLED_ShowNum(2, 7, lightStatus, 1);
	
	while(1) {
		lightStatus = LightSensor_GetStatus();
		OLED_ShowNum(2, 7, lightStatus, 1);
		
		if (lightStatus && isOpen) {
			for(int i = 500; i >= 0; i --) {
				level = i * 2;
				PWM_OutPut1_level(level);
				PWM_OutPut2_level(level);
				OLED_ShowNum(1, 7, level, 4);
			}
			OLED_ShowString(3, 1, "Close");
			isOpen = 0;
		}
		if (!lightStatus && !isOpen) {
			for(int i = 0; i <= 250; i ++) {
				level = i * 4;
				PWM_OutPut1_level(level);
				PWM_OutPut2_level(level);
				OLED_ShowNum(1, 7, level, 4);
			}
			OLED_ShowString(3, 1, "Open ");
			isOpen = 1;
		}
	}
}
